package com.ly.cloud.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ly.cloud.dto.CourseTempDetailDto;
import com.ly.cloud.dto.CourseTempDto;
import com.ly.cloud.entity.CourseTempDetailPo;
import com.ly.cloud.entity.CourseTempPo;
import com.ly.cloud.exception.CloudException;
import com.ly.cloud.exception.biz.BusinessException;
import com.ly.cloud.exception.mapper.DBException;
import com.ly.cloud.mapper.CourseTimeDetailMapper;
import com.ly.cloud.mapper.CourseTimeMapper;
import com.ly.cloud.remote.feign.ApprovalServiceFeign;
import com.ly.cloud.remote.feign.CodeRuleServiceFeign;
import com.ly.cloud.service.CourseTimeService;
import com.ly.cloud.utils.BeanCopyUtils;
import com.ly.cloud.vo.CourseTempCasVo;
import com.ly.cloud.vo.CourseTempDetailVo;
import com.ly.cloud.vo.CourseTempVo;
import com.ly.cloud.web.utils.WebResponse;


/**
 * 课程学时模板Impl
 * @author caihuihai
 * @date 2018年12月17日 下午4:31:45
 */
@Service
public class CourseTimeServiceImpl implements CourseTimeService {
	
	@Autowired
	private CodeRuleServiceFeign codeRuleServiceFeign;  //测试Feign
	@Autowired
	private CourseTimeMapper courseMapper;  //课程模板
	@Autowired
	private CourseTimeDetailMapper detailMapper;  //课程模板学时详情
	@Autowired
	private ApprovalServiceFeign approvalFeign;  //审批流程Feign
	

	@Override
	public String testGen(String loginUserId,String loginUserName,String bbh) {
		System.out.println("dddddddd"+loginUserId);
		WebResponse<String> wcode = codeRuleServiceFeign.genRuleCode(bbh);
		System.out.println("wcode"+wcode.getData());
		//WebResponse<String> wcode2 = approvalFeign.initApproval("01010101001","LY_CKBZ_GXJW_KCSJSQL");
		WebResponse<String> wcode4 = approvalFeign.genRuleCode(bbh);
		System.out.println("wcode4"+wcode4.getData());
		WebResponse<String> wcode3 = approvalFeign.processApproval("01010101001","LY_CKBZ_GXJW_KCSJSQL", "L00088", "呵呵", "1");
		System.out.println("wcode3"+wcode3.getData());
		return null;
	}

	/**
	 * 分页查询课程学时模板列表
	 */
	@Override
	public Page<CourseTempVo> selectPage(Page<CourseTempVo> page) {
		try {
			return page.setRecords(courseMapper.selectPage(page));
		} catch (Exception e) {
			throw new CloudException("查询异常，请重试!", e);
		}
	}

	/**
	 * 保存课程模板
	 */
	@Transactional
	@Override
	public boolean submitMb(CourseTempDto dto, String loginUserName) {
		try {
			int tag = 0;
			if(StringUtils.isBlank(dto.getType())) {
				throw new BusinessException();
			}
			CourseTempPo po = new CourseTempPo();
			BeanUtils.copyProperties(dto, po);
			//如果是默认模板，则先把其他所有的设置为非默认模板
			if(StringUtils.isNoneBlank(dto.getSfmr()) && "1".equals(dto.getSfmr())) {
				updateAllMr();
			}
			if(dto.getType().equals("add")) {  //新增
				po.setCjr(loginUserName);
				po.setCjsj(new Date());
				tag = courseMapper.insert(po);
			} else {  //修改
				po.setXgr(loginUserName);
				po.setXgsj(new Date());
				EntityWrapper<CourseTempPo> ew = new EntityWrapper<>();
				ew.where("kcmbbh={0}", po.getKcmbbh());
				tag = courseMapper.update(po, ew);
			}
			return tag>0?true:false;
		} catch (BusinessException e) {
			throw new BusinessException("请传type操作类型值", e);
		} catch (Exception e) {
			throw new DBException("操作异常，请重试!", e);
		}
	}

	/**
	 * 更新所有的数据为非默认模板
	 */
	private void updateAllMr() {
		try {
			CourseTempPo po = new CourseTempPo();
			po.setSfmr("0");
			courseMapper.update(po, null);
		} catch (Exception e) {
			throw new DBException("更新所有模板为非默认状态时失败！", e);
		}
	}

	/**
	 * 根据课程模板编号查询模板信息
	 */
	@Override
	public CourseTempVo findMbByBh(Long kcmbbh) {
		try {
			CourseTempVo vo = new CourseTempVo();
			CourseTempPo po = new CourseTempPo();
			po.setKcmbbh(kcmbbh);
			CourseTempPo selPo = courseMapper.selectOne(po);
			BeanUtils.copyProperties(selPo, vo);
			return vo;
		} catch (Exception e) {
			throw new DBException("查询数据失败！", e);
		}
	}

	/**
	 * 删除课程模板时并把对应的课程学时信息也删除
	 */
	@Transactional
	@Override
	public boolean delMbByBh(Long kcmbbh) {
		try {
			//1、删除对应的学时信息
			EntityWrapper<CourseTempDetailPo> detailEw = new EntityWrapper<>();
			detailEw.where("kcmbbh={0}", kcmbbh);
			detailMapper.delete(detailEw);
			//2、删除课程模板
			EntityWrapper<CourseTempPo> ew = new EntityWrapper<>();
			ew.where("kcmbbh={0}", kcmbbh);
			courseMapper.delete(ew);
			return true;
		} catch (Exception e) {
			throw new DBException("删除课程模板失败！", e);
		}
	}

	/**
	 * 根据课程模板编号查询学时列表
	 */
	@Override
	public List<CourseTempDetailVo> queryKcxsList(Long kcmbbh) {
		try {
			EntityWrapper<CourseTempDetailPo> ew = new EntityWrapper<>();
			ew.where("kcmbbh={0}", kcmbbh).orderBy("xsbh", true);
			List<CourseTempDetailPo> poList = detailMapper.selectList(ew);
			return BeanCopyUtils.copyListProperties(poList, CourseTempDetailVo.class);
		} catch (Exception e) {
			throw new CloudException("查询异常，请重试!", e);
		}
	}

	/**
	 * 点击新增学时按钮时的查询代码集list，需要过滤已选择的学时
	 */
	@Override
	public Page<CourseTempDetailVo> queryKcxsDmjPage(Page<CourseTempDetailVo> page, Long kcmbbh, String xsbh,
			String xsmc) {
		try {
			return page.setRecords(detailMapper.queryKcxsDmjPage(page,kcmbbh,xsbh,xsmc));
		} catch (Exception e) {
			throw new CloudException("查询异常，请重试!", e);
		}
	}

	/**
	 * 保存新增的学时信息
	 */
	@Override
	public boolean insertMbxs(List<CourseTempDetailDto> list) {
		try {
			for(CourseTempDetailDto dto : list) {
				CourseTempDetailPo po = new CourseTempDetailPo();
				BeanUtils.copyProperties(dto, po);
				detailMapper.insert(po);
			}
			return true;
		} catch (Exception e) {
			throw new DBException("保存学时信息失败！", e);
		}
	}

	/**
	 * 删除学时信息
	 */
	@Override
	public boolean deleteKcxs(Long kcmbbh, String xsbh) {
		try {
			EntityWrapper<CourseTempDetailPo> ew = new EntityWrapper<>();
			ew.where("kcmbbh={0} and xsbh={1}", kcmbbh,xsbh);
			return detailMapper.delete(ew)>0?true:false;
		} catch (Exception e) {
			throw new DBException("删除学时信息失败！", e);
		}
	}

	/**
	 * 查询课程学时级联列表
	 */
	@Override
	public List<CourseTempCasVo> findKcxsCasList() {
		try {
			List<CourseTempCasVo> list =  new ArrayList<CourseTempCasVo>();
			//课程模板list
			list = BeanCopyUtils.copyListProperties(courseMapper.selectList(null), CourseTempCasVo.class); 
			for (CourseTempCasVo courseTempCasVo : list) {
				List<CourseTempDetailVo> xsList =  new ArrayList<CourseTempDetailVo>();
				//根据课程模板编号查询对应的学时详情list
				EntityWrapper<CourseTempDetailPo> ew = new EntityWrapper<>();
				ew.where("kcmbbh={0}", courseTempCasVo.getKcmbbh());
				xsList = BeanCopyUtils.copyListProperties(detailMapper.selectList(ew), CourseTempDetailVo.class);
				courseTempCasVo.setXsList(xsList);
			}
			return list;
		} catch (Exception e) {
			throw new CloudException("查询异常，请重试!", e);
		}
	}

	

}
